<template>
  <div>
    <my-breadcrumb :items="['系统管理', '引导页']"/>
    <search :formItems="searchItems"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">新增引导页</el-button>
    </div>
    <el-table :data="starts">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="是否展示"
        prop="status">
        <template slot-scope="scope">
          {{ scope.row.status === '1' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        prop="updatetime">
      </el-table-column>
      <el-table-column
        label="图片"
        prop="img">
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="" height="40">
        </template>
      </el-table-column>
      <!--<el-table-column
        label="展示时间"
        prop="showtime">
      </el-table-column>-->
      <el-table-column
        label="操作"
        prop="id">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="isAdd ? '新增引导页' : '信息修改'" :visible.sync="showAdd">
      <el-form :model="start" label-width="70px" ref="addForm">
        <el-form-item label="是否展示" prop="status">
          <el-select v-model="start.status">
            <el-option value="1" label="是"/>
            <el-option value="0" label="否"/>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <img v-if="start.img" :src="start.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {deleteRow} from '../../utils/getList'
export default {
  name: 'start',
  data () {
    return {
      searchItems: [
      ], // 检索结构
      showAdd: false,
      isAdd: true,
      starts: [],
      start: {
        status: '1',
        img: ''
      },
      updateRow: null
    }
  },
  methods: {
    getList () {
      this.axios.get('/Config/getguide')
        .then(res => {
          this.starts = res.data.data.list
        })
    },
    beforeUpload (file) {
      let reader = new FileReader()
      let _self = this
      reader.onload = function (ev) {
        _self.start.img = ev.currentTarget.result
      }
      reader.readAsDataURL(file)
      return false
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.$nextTick(() => {
        Object.keys(this.start).forEach(key => {
          this.start[key] = row[key]
        })
      })
    },
    toSubmit () {
      let url = '/Config/addguide'
      let params = {}
      Object.keys(this.start).forEach(key => {
        params[key] = this.start[key]
      })
      if (!this.isAdd) {
        url = '/Config/editguide'
        params.id = this.updateRow.id
      }
      this.axios.post(url, this.qs.stringify(params))
        .then(res => {
          if (res.data.status === 1) {
            this.getList()
            this.$message.success(res.data.msg)
            this.showAdd = false
          }
        })
    },
    toDelete (id) {
      deleteRow({url: '/Config/delguide', id}, res => {
        if (res.data.status === '1' || res.data.status === 1) {
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>
  .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
